import employees from '@/assets/constant/employees'
import store from '@/store'

export default {
  imgerror: {
    inserted(dom, obj, vnode) {
      dom.src = dom.src || obj.value
      dom.onerror = function() {
        dom.src = obj.value
      }
    },
    componentUpdated(dom, obj) {
      dom.src = dom.src || obj.value
    },
    unbind(dom, obj) {
      dom.onerror = null
    }
  },
  hireType: {
    inserted(dom, obj) {
      const result = employees.hireType.forEach((item) => {
        return item.id === obj.value
      })
      dom.innerText = result ? result.value : '-'
    },
    componentUpdated(dom, obj) {
      const result = employees.hireType.forEach((item) => {
        return item.id === obj.value
      })
      dom.innerText = result ? result.value : '-'
    }
  },
  clickOutSide: {
    inserted(dom, obj, vnode) {
      const fn = (e) => {
        if (!dom.contains(e.target)) {
          const callback = obj.value
          callback()
        }
      }
      vnode.fn = fn
      document.addEventListener('click', fn)
    },
    unbind(dom, obj, vnode) {
      document.removeEventListener('click', vnode.fn)
    }
  },
  control: {
    inserted(dom, obj) {
      // 假如存在该值取反    则隐藏按钮
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    },
    componentUpdated(dom,obj){
      if (!store.state.user.userInfo.roles.points.includes(obj.value)) {
        dom.style.display = 'none'
      }
    }
  }
}
